﻿using Max.PH4.Shoppgin.Admin.Domain;
using Max.PH4.Shoppgin.Admin.IRespository;
using Max.PH4.Shoppgin.Admin.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH4.Shoppgin.Admin.Service
{
    public class LogisticMasterService : ILogisticMasterService
    {
        ILogisticMasterRespository _logisticMasterRespository;
        IUnitOfWork _unitOfWork;

        public LogisticMasterService(ILogisticMasterRespository logisticMasterRespository, IUnitOfWork unitOfWork)
        {
            _logisticMasterRespository = logisticMasterRespository;
            _unitOfWork = unitOfWork;
        }

        public async Task<int> DeleteAsync(LogisticMasterEntity entity)
        {
            _logisticMasterRespository.Delete(entity);
            int result = await _unitOfWork.SaveChangesAsync();
            return result;
        }

        public async Task<int> DeleteAsync(object id)
        {
            _logisticMasterRespository.Delete(id);
            int result = await _unitOfWork.SaveChangesAsync();
            return result;
        }

        public async Task<int> InsertAsync(LogisticMasterEntity entity)
        {
            _logisticMasterRespository.Insert(entity);
            int result = await _unitOfWork.SaveChangesAsync();
            return result;
        }


        public async Task<IQueryable<LogisticMasterEntity>> QueryAsync(string orderNo)
        {
            IQueryable<LogisticMasterEntity> list = null;
            await Task.Run(() =>
            {
                list = _logisticMasterRespository.Query().Where(x => x.OrderNo.Contains(orderNo));
            });
            return list;
        }

        public async Task<int> UpdateAsync(LogisticMasterEntity entity)
        {
            _logisticMasterRespository.Update(entity);
            int result = await _unitOfWork.SaveChangesAsync();
            return result;

        }

        public async Task<int> UpdateAsync(List<LogisticMasterEntity> entities)
        {
            _logisticMasterRespository.Update(entities);
            int result = await _unitOfWork.SaveChangesAsync();
            return result;
        }

    }
}
